<template>
	<!-- 热更新弹窗 -->
		
	<view class="wrap" v-if="popup_show">
		<view class="popup-bg">
			<view class="popup-content" :class="{'popup-content-show' : popup_show}">
				<view class="update-wrap">
					<image src="./images/img.png" class="top-img"></image>
					<view class="contentpop">
						<text class="title" v-if="versondata.content">{{ versondata.content }}</text>
						<!-- 升级描述 -->
						<!-- <view class="title-sub" v-html="versondata.update_text"></view> -->
						<!-- 升级按钮 -->
						<button class="btn" @click="todown()">立即升级</button>
						<!-- 下载进度 -->
						<!-- <view class="sche-wrap" v-else>
							<view class="sche-bg">
								<view class="sche-bg-jindu" :style="lengthWidth"></view>
							</view>
							<text
								class="down-text">下载进度:{{downSize+"%"}}</text>
						</view> -->
					</view>
				</view>
				<!-- <image src="./images/close.png" class="close-ioc" @click="closeUpdate()" v-if="versondata.is_force == 0"></image> -->
			</view>
	
		</view>
	</view>
</template>

<script>
export default {
	props: {
		popup_show: {
			type: Boolean,
			default: false
		},
		versondata:{
			type: Object,
			default: {}
		}
	},
	name: "Down",
	data() {
		return {
			// popup_show:true,
			platform: "", //ios or android
			version: "1.0.0", //当前软件版本
			need_update: false, // 是否更新
			downing: false, //是否下载中
			downstatus: 0, //0未下载  1已开始 2已连接到资源  3已接收到数据  4下载完成
			update_info: {
				os: '', //设备系统
				version: '', //最新版本
				note: '', //升级说明
			},
			fileSize: 0, //文件大小
			downSize: 0, //已下载大小
			viewObj: null, //原生遮罩vie
			// versondata:{},
		};
	},
	created(){
		// this.getfgetVersion();//版本更新
	},
	methods: {
		async getfgetVersion(){
			let res = await this.$u.api.getVersion({
			});
			this.versondata = res
		},
		getsou() {
			this.$emit("getsou",this.keyword);
		},
		connectWallet(){
			
			this.$emit("connectWallet");
		},
		todown() {
			let that = this
			let platform = uni.getSystemInfoSync().platform;
			// let url = 'https://mp.xiangduohui.com/down/XDH.apk'
			let url = this.versondata.downloadurl
			if (platform == 'ios') {
				if (this.ginfo.download_ios_url != '') {
					//跳转ios下载地址
					plus.runtime.openURL(url, function (res) {
						uni.showModal({
							content: '本机未检测到对应APP，是否打开浏览器访问页面？',
							success: function (res) {
								if (res.confirm) {
									plus.runtime.openURL(url);
								}
							}
						});
					});
				} else {
					plus.runtime.openURL(url, function (res) {
						uni.showModal({
							content: '本机未检测到对应APP，是否打开浏览器访问页面？',
							success: function (res) {
								if (res.confirm) {
									plus.runtime.openURL(url);
								}
							}
						});
					});
				}
			} else {
				if (url != '') {
					
					//安卓直接下载地址
					this.doUpData(url)
				} else {
					let puurl = 'https://www.pgyer.com/1roGoEy0'
					plus.runtime.openURL(puurl, function (res) {
						uni.showModal({
							content: '本机未检测到对应APP，是否打开浏览器访问页面？',
							success: function (res) {
								if (res.confirm) {
									plus.runtime.openURL(that.ginfo.download_url);
								}
							}
						});
					});
				}

			}
		},
		doUpData(Url) {
			
			// uni.showLoading({
			// 	title: '已更新'+this.downloadNum+'%'
			// }) 
			console.log(Url)
			var showLoading = plus.nativeUI.showWaiting('正在下载');
			const downloadTask = uni.downloadFile({ //执行下载
				url: Url, //下载地址
				timeout: 1000 * 30, //30秒超时时间
				success: downloadResult => { //下载成功
					
					this.showdownLine = false
					plus.nativeUI.closeWaiting();
					if (downloadResult.statusCode == 200) {
						plus.runtime.install( //安装软件
							downloadResult.tempFilePath, {
							force: true
						},
							function (res) {
								plus.runtime.restart();
							}
						);
					}
				},
				fail: err => {
					plus.nativeUI.closeWaiting();
					this.showdownLine = false
					this.showToast(err.errMsg)
					
				},
				complete: com => {
					
				}
			});

			// 下载进度
			downloadTask.onProgressUpdate(res => {
				// this.$u.toast(res.progress)
				this.downloadNum = res.progress
				
				showLoading.setTitle('正在下载，进度：' + res.progress +'%');
				// 
				// 

				// 满足测试条件，取消下载任务。
				// if (res.progress > 50) {
				//  downloadTask.abort();
				// }
			});
		},
	}
}
</script>

<style scoped lang="scss">
// app热更新
.popup-bg {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
		position: fixed;
		top: 0;
		left: 0rpx;
		right: 0;
		bottom: 0;
		width: 750rpx;
    z-index: 10000;
		background-color: rgba(0, 0, 0, .6);
	}
	
	.popup-content {
		display: flex;
		flex-direction: column;
		align-items: center;
	}
	
	.popup-content-show {
		animation: mymove 500ms;
		transform: scale(1);
	}
	
	@keyframes mymove {
		0% {
			transform: scale(0);
			/*开始为原始大小*/
		}
	
		100% {
			transform: scale(1);
		}
	
	}
	
	
	
	.update-wrap {
		width: 580rpx;
		border-radius: 18rpx;
		position: relative;
		display: flex;
		flex-direction: column;
		background-color: #ffffff;
		padding: 170rpx 30rpx 0;
	
		.top-img {
			position: absolute;
			left: 0;
			width: 100%;
			height: 256rpx;
			top: -128rpx;
		}
	
		.contentpop {
			display: flex;
			flex-direction: column;
			align-items: center;
			padding-bottom: 40rpx;
	
			.title {
				font-size: 32rpx;
				font-weight: bold;
				color: #6526f3;
			}
	
			.title-sub {
				text-align: center;
				font-size: 24rpx;
				color: #666666;
				padding: 30rpx 0;
			}
	
			.btn {
				width: 460rpx;
				display: flex;
				align-items: center;
				justify-content: center;
				color: #ffffff;
				font-size: 30rpx;
				height: 80rpx;
				line-height: 80rpx;
				border-radius: 100px;
				background-color: #6526f3;
				margin-top: 20rpx;
			}
		}
	}
	
	
	.close-ioc {
		width: 70rpx;
		height: 70rpx;
		margin-top: 30rpx;
	}
	
	.sche-wrap {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: flex-end;
		padding: 10rpx 50rpx 0;
	
		.sche-wrap-text {
			font-size: 24rpx;
			color: #666;
			margin-bottom: 20rpx;
		}
	
		.sche-bg {
			position: relative;
			background-color: #cccccc;
			height: 30rpx;
			border-radius: 100px;
			width: 480rpx;
			display: flex;
			align-items: center;
	
			.sche-bg-jindu {
				position: absolute;
				left: 0;
				top: 0;
				height: 30rpx;
				min-width: 40rpx;
				border-radius: 100px;
				background: url(images/round.png) #5775e7 center right 4rpx no-repeat;
				background-size: 26rpx 26rpx;
			}
		}
	
		.down-text {
			font-size: 24rpx;
			color: #5674e5;
			margin-top: 16rpx;
		}
	}
  .downview{
    position: fixed;
    bottom: 40%;
    right: 30rpx;
    z-index: 10000;
    .xaizai{
      width: 96rpx;
      height: 120rpx;
    }
    .chadown{
      position: absolute;
      right: -5px;
      top: -15px;

    }
  }
</style>
